using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text.Json;

namespace FeishuWorkflowService.Models
{
    /// <summary>
    /// 飞书配置
    /// </summary>
    public class FeishuConfig
    {
        // 新增：配置文件中的节点名（与appsettings.json中的键一致）
        public const string SectionName = "FeishuConfig";
        public string AppId { get; set; } = string.Empty;
        public string AppSecret { get; set; } = string.Empty;
        public string BaseUrl { get; set; } = string.Empty;
        public string WorkflowCallbackUrl { get; set; } = string.Empty;
    }

    /// <summary>
    /// 服务配置
    /// </summary>
    public class ServiceConfig
    {
        public string ApiBaseUrl { get; set; } = string.Empty;
        public string ApiKey { get; set; } = string.Empty;
    }


    public class FeishuReposeBase
    {
        [JsonProperty("code")]
        public int Code { get; set; }

        [JsonProperty("msg")]
        public string? Msg { get; set; }
    }


    /// <summary>
    /// 飞书令牌响应
    /// </summary>
    public class FeishuTokenResponse : FeishuReposeBase
    {

        [JsonProperty("tenant_access_token")]
        public string TenantAccessToken { get; set; } = string.Empty;

        [JsonProperty("expire")]
        public int Expire { get; set; }
    }

    /// <summary>
    /// 飞书工作流响应
    /// </summary>
    public class FeishuWorkflowResponse : FeishuReposeBase
    {

        [JsonProperty("data")]  // 与JSON的"data"匹配
        public FeishuWorkflowData Data { get; set; } = new FeishuWorkflowData();
    }


    /// <summary>
    /// 飞书工作流数据
    /// </summary>
    public class FeishuWorkflowData
    {
        // 关键修复：JsonProperty严格匹配JSON的"instance_code"
        [JsonProperty("instance_code")]
        public string Instance_code { get; set; } = string.Empty;  // C#属性名用PascalCase（符合规范）
    }

    /// <summary>
    /// 飞书工作流状态响应
    /// </summary>
    public class FeishuWorkflowStatusResponse : FeishuReposeBase
    {

        [JsonProperty("data")]
        public FeishuWorkflowStatusData Data { get; set; } = new FeishuWorkflowStatusData();
    }

    /// <summary>
    /// 飞书工作流状态数据
    /// </summary>
    public class FeishuWorkflowStatusData
    {
        [JsonProperty("approval_code")]
        public string Approval_code { get; set; } = string.Empty;

        [JsonProperty("approval_name")]
        public string Approval_name { get; set; } = string.Empty;

        [JsonProperty("department_id")]
        public string Department_id { get; set; } = string.Empty;

        [JsonProperty("end_time")]
        public string End_time { get; set; } = string.Empty;

        [JsonProperty("form")]
        public string Form { get; set; } = string.Empty;


        [JsonProperty("instance_code")]
        public string Instance_code { get; set; } = string.Empty;

        [JsonProperty("open_id")]
        public string Open_id { get; set; } = string.Empty;

        [JsonProperty("reverted")]
        public string Reverted { get; set; } = string.Empty;

        [JsonProperty("serial_number")]
        public string Serial_number { get; set; } = string.Empty;

        [JsonProperty("start_time")]
        public string Start_time { get; set; } = string.Empty;

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("task_list")]
        public List<object?> Task_list { get; set; } = new List<object?>();

        [JsonProperty("timeline")]
        public List<object?> Timeline { get; set; } = new List<object?>();

        [JsonProperty("user_id")]
        public string User_id { get; set; }

        [JsonProperty("uuid")]
        public string Uuid { get; set; }
       

    }

    /// <summary>
    /// 飞书工作流模板响应
    /// </summary>
    public class FeishuWorkflowTemplateResponse : FeishuReposeBase
    {

        [JsonProperty("data")]
        public FeishuWorkflowTemplateData Data { get; set; } = new FeishuWorkflowTemplateData();
    }

    public class FeishuWorkflowTemplateData
    {
        [JsonProperty("approval_name")]
        public string Approval_name { get; set; }

        [JsonProperty("form")]
        public string Form { get; set; }

        [JsonProperty("form_widget_relation")]
        public string Form_widget_relation { get; set; }

        [JsonProperty("node_list")]
        public List<object?> node_list { get; set; } = new List<object?>();

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("viewers")]
        public List<object?> Viewers { get; set; } = new List<object?>();

    }

    /// <summary>
    /// 创建工作流请求
    /// </summary>
    public class CreateWorkflowRequest
    {
        /// <summary>
        /// 审批定义Code
        /// </summary>
        public string ApprovalCode { get; set; } = string.Empty;

        /// <summary>
        /// 操作人ID
        /// </summary>
        public string OperatorId { get; set; } = string.Empty;

        /// <summary>
        /// 审批人用户ID列表
        /// </summary>
        public Dictionary<string, List<string>> ApproverUserIds { get; set; } = new Dictionary<string, List<string>>();

        /// <summary>
        /// 表单数据
        /// </summary>
        public Dictionary<string, object>? FormData { get; set; }
    }


    public class Widget
    {
        public string custom_id { get; set; }
        public string default_value_type { get; set; }
        public object display_condition { get; set; } // 可以根据实际类型修改
        public bool enable_default_value { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public bool printable { get; set; }
        public bool required { get; set; }
        public string type { get; set; }
        public bool visible { get; set; }
        public string widget_default_value { get; set; }

        public object option { get; set; }

        public List<Widget>? children { get; set; }
    }
    public class FormItem
    {
        public string id { get; set; } = string.Empty;
        public string type { get; set; } = string.Empty;
        public object value { get; set; } = string.Empty;

    }

    /// <summary>
    /// 工作流模板数据
    /// </summary>
    public class WorkflowTemplate
    {
        public string? ApprovalTemplateName { get; set; }

        public string? ApprovalTemplateNode { get; set; }

        public List<Widget>? FormTemplateData { get; set; }
    }

    /// <summary>
    /// 工作流状态
    /// </summary>
    public class WorkflowStatus
    {
        /// <summary>
        /// 实例ID
        /// </summary>
        public string InstanceId { get; set; } = string.Empty;

        /// <summary>
        /// 状态
        /// </summary>
        public string Status { get; set; } = string.Empty;

        /// <summary>
        /// 审批定义Code
        /// </summary>
        public string ApprovalCode { get; set; } = string.Empty;

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 表单数据
        /// </summary>
        public string FormData { get; set; } = string.Empty;

        /// <summary>
        /// 流水号
        /// </summary>
        public string Serialnumber { get; set; } = string.Empty;
    }

    /// <summary>
    /// 飞书流程状态枚举
    /// </summary>
    public enum FeishuApproveStatus
    {
        /*
         * PENDING     审批中	                    ❌ 未完成
         * APPROVED    审批通过	                    ✅ 已完成
         * REJECTED    审批拒绝	                    ✅ 已完成
         * CANCELED    审批已撤回	                    ✅ 已完成
         * TERMINATED  审批被终止（如管理员删除）	    ✅ 已完成
         */


        PENDING,
        APPROVED,
        REJECTED,
        CANCELED,
        TERMINATED
    }
}